{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Word2Vec 模型之 CBOW(continuous bag of words)\n",
    "\n",
    "## 问题抽象\n",
    "\n",
    "$$g(w) = \\prod_{u \\in w \\cup NEG(w)} p(u|Context(w))$$\n",
    "\n",
    "$$p(u|Context(w)) = \\sigma(X_w^T\\theta^u)^{L^w(u)} (1 - \\sigma(X_w^T\\theta^u))^{(1 - L^w(u))}$$\n",
    "\n",
    "## 损失函数\n",
    "\n",
    "$$Loss = log(g(w))$$\n",
    "\n",
    "$$Loss = \\sum (L^w(u) * log(\\sigma(X_w^T\\theta^u)) + (1 - L^w(u)) * (1 - \\sigma(X_w^T\\theta^u)))$$\n",
    "\n",
    "## 梯度\n",
    "\n",
    "$$\\frac{\\partial Loss}{\\partial \\theta^u} = (L^w(u) - \\delta (X_w^T\\theta^u)) X_w$$\n",
    "\n",
    "$$\\frac{\\partial Loss}{\\partial X^w} = (L^w(u) - \\delta (X_w^T\\theta^u)) \\theta^u$$\n",
    "\n",
    "$$\\theta^u = \\theta^u + \\alpha * \\frac{\\partial Loss}{\\partial \\theta^u}$$\n",
    "\n",
    "$$v(w_{context}) = v(w_{context}) + \\sum_{u \\in w \\cup NEG(w)} \\alpha * \\frac{\\partial Loss}{\\partial X^w}$$\n",
    "\n",
    "## 训练主流程\n",
    "\n",
    "- 选取中心词w以及负采样出的NEG(w)\n",
    "\n",
    "- 分别获得损失函数对于$x_w$与$\\theta^u$的梯度\n",
    "\n",
    "- 更新$\\theta^u$以及中心词对应的context(w)的每一个词的词向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
